package com.bestcem.xm.member.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.member.dao.BaseDao;
import com.bestcem.xm.member.dao.ExportRecordDao;
import com.bestcem.xm.member.dao.mapper.ExportRecordMapper;
import com.bestcem.xm.member.entity.mysql.ExportRecordEntity;
import com.bestcem.xm.member.entity.pojo.ExportRecordDO;
import com.bestcem.xm.member.enums.MemberTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 导出记录
 * @date 2021-07-24
 */
@Repository("exportRecordDao")
public class ExportRecordDaoImpl extends BaseDao implements ExportRecordDao {

    @Autowired
    private ExportRecordMapper exportRecordMapper;

    @Override
    public long countByUser(String orgId, String opUserId, MemberTypeEnum memberTypeEnum) {
        return exportRecordMapper.countByUser(orgId, opUserId, memberTypeEnum.getIndex());
    }

    @Override
    public List<ExportRecordDO> listByUser(String orgId, String opUserId, MemberTypeEnum memberTypeEnum, long skip, int limit) {
        List<ExportRecordEntity> entities = exportRecordMapper.listByUser(orgId, opUserId, memberTypeEnum.getIndex(), skip, limit);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(
                    entity -> ExportRecordDO.toDo(entity)
            ).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public ExportRecordDO findById(String orgId, String id) {
        ExportRecordEntity entity = exportRecordMapper.findOneByIdAndOrgId(id, orgId);
        return ExportRecordDO.toDo(entity);
    }

    @Override
    public Integer updateStatusById(ExportRecordDO exportRecord) {
        return exportRecordMapper.updateStatusById(ExportRecordDO.toEntity(exportRecord));
    }

    @Override
    public Integer batchDeleteByIds(String orgId, List<String> ids) {
        exportRecordMapper.batchDeleteByIds(orgId, ids);
        return 1;
    }

    @Override
    public void updateStatusAndFile(ExportRecordDO exportRecord) {
        exportRecordMapper.updateStatusAndFile(ExportRecordDO.toEntity(exportRecord));
    }

    @Override
    public void updateStatusAndMsgById(String id, int status, String msg) {
        exportRecordMapper.updateStatusAndMsgById(id, status, msg);
    }

    @Override
    public void saveRecord(ExportRecordDO exportRecord) {
        exportRecordMapper.saveRecord(ExportRecordDO.toEntity(exportRecord));
    }
}
